import {Page, Pageable} from "../utils/Page";
import fetcher from "../apis/Fetcher";
import {NumberIdAndName} from "../types/BasicModels";
import {OffsetDateTime} from "@js-joda/core";

export interface AddGoodsRequest {
    name: String
    amount: number
    price: number
    barcode: string
    factoryId?: number
    categoryId?: number
    brandId?: number
    boxSpecification?: string
    costPrice?: number
    units: EditGoodsUnitRequest[]
    registrationCertificateNumber?: string
    recordFilingNumber?: string
    expressionPeriod?: string
    note?: string
    executiveStandard?: string
    subGoodsList: Array<EditSubGoodsRequest>
}

export interface EditSubGoodsRequest {
    id?: number
    name: string
    amount: number
    number: string
}

export interface EditGoodsUnitRequest {
    name: string
    basicMultiple: number
    id?: number
    updatable: boolean
}

export function addGoods(addGoodsRequest: AddGoodsRequest) {
    return fetcher.post("goods", {}, addGoodsRequest)
}

export interface QueryGoodsRequest {
    nameOrBarcode?: string
    name?: string
    barcode?: string
    categoryIds?: number[]
    brandIds?: number[]
    factoryIds?: number[]
    editMarketOrderId?: number
    editProcurementId?: number
    editMarketReturnOrderId?: number
    editMarketSaleDocumentId?: number
    editProcurementReceiptId?:number
    excludeIds?: number[]
}

export function queryGoods(queryGoodsRequest: QueryGoodsRequest, pageable: Pageable) {
    return fetcher.get<Page<QueryGoodsResponse>>("goods", {...queryGoodsRequest, ...pageable.toRequestParams()})
}

export interface QueryGoodsResponse {
    name: string
    amount: number
    price: number
    barcode: string
    createdDateTime: OffsetDateTime
    lastModifiedDateTime: OffsetDateTime
    id: number
    brand?: NumberIdAndName
    category?: NumberIdAndName
    factory?: NumberIdAndName
    boxSpecification?: string
    costPrice?: number
    units: Array<GoodsUnitResponse>
    registrationCertificateNumber?: string
    recordFilingNumber?: string
    expressionPeriod?: string
    subGoodsList: Array<SubGoodsResponse>
}

export interface AddOrderItemKey {
    goodsId: number
    subGoodsId?: number
}

export interface SubGoodsResponse {
    amount: number
    id: number
    name: string
    number: string
}

export interface GoodsUnitResponse {
    id: number
    name: string
    basicMultiple: number
    updatable: boolean
}

export interface QueryGoodsUnitResponse {
    id: number
    name: string
    basicMultiple: number
}

export interface ExportGoodsToExcelRequest extends QueryGoodsRequest {
    fields?: string[]
}

// export function exportGoodsToExcel(queryGoodsRequest: ExportGoodsToExcelRequest, sort?: Sort) {
//     return fetcher.get<Blob>("goods/excel", {...queryGoodsRequest, sort: sort?.toRequestParam(),}, {responseType: 3})
// }

export function updateGoods(id: number, addGoodsRequest: AddGoodsRequest) {
    return fetcher.put(`goods/${id}`, {}, addGoodsRequest)
}


export function getAllGoodsSummaries(request?: QueryGoodsRequest) {
    return fetcher.get<GoodsSummary[]>("goods", {
        projection: "summary",
        ...request,
    })
}

export interface GoodsSummary {
    id: number
    name: string
    barcode: string
}

export function importGoodsFromExcel(file: File) {
    let formData = new FormData();
    formData.append("file", file)
    return fetcher.post<number>("goods/excel/import", {}, formData, {
        timeout: 30000
    })
}

export function downloadImportTemplate() {
    // return fetcher.get<Blob>("goods/excel/import/template", {}, {responseType: 3})
}

export function getGoodsDetail(id: number) {
    return fetcher.get<GoodsDetailResponse>(`goods/${id}`)
}

export function getGoodsDetailByBarcode(barcode: string) {
    return fetcher.get<GoodsDetailResponse>(`goods/barcode/${barcode}`)
}

export interface GoodsDetailResponse {
    name: string
    amount: number
    price: number
    barcode: string
    createdDateTime: OffsetDateTime
    lastModifiedDateTime: OffsetDateTime
    id: number
    brand?: NumberIdAndName
    category?: NumberIdAndName
    factory?: NumberIdAndName
    boxSpecification?: string
    lastCheckInventoryRecordItem?: LastCheckInventoryRecordItem
    units: Array<GoodsUnitResponse>
    registrationCertificateNumber?: string
    recordFilingNumber?: string
    expressionPeriod?: string
    executiveStandard?: string
    note?: string
    subGoodsList: SubGoodsResponse[]
}

export interface LastCheckInventoryRecordItem {
    checkInventoryRecordId: number
    amount: number
    createdDateTime: OffsetDateTime
}

export function getAllGoodsWithMarketInStore(marketId: number) {
    return fetcher.get<GoodsWithMarketInStore[]>("goods", {
        marketId
    })
}

export function getAllGoodsWithMarketInStoreInMakret(marketId: number) {
    return fetcher.get<GoodsWithMarketInStore[]>("goods", {
        marketId,
        onlyInMarket: true
    })
}

export interface GoodsWithMarketInStore {
    id: number
    name: string
    amount: number
    barcode: string
    amountInMarket: number
}

export function deleteGoods(id: number) {
    return fetcher.delete(`goods/${id}`)
}

export function getAllGoodsUnits() {
    return fetcher.get<string[]>("goods/units")
}

export function isGoodsBarcodeExists(barcode: string) {
    return fetcher.get<boolean>(`/goods/barcodes/${barcode}/exists`)
}